/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.koron.workflow.api.bean.HistoryTaskMsgBean;
import com.koron.workflow.api.mapper.HistoryTaskMsgMapper;
import com.koron.workflow.api.service.ActivitiService;
import com.koron.workflow.api.service.HistoryTaskMsgService;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.WorkflowUtil;

@Service
public class HistoryTaskMsgServiceImpl implements HistoryTaskMsgService{
	
	@Autowired
	private ActivitiService activitiService;
	
	/**
	 * 添加流程审批历史记录
	 */
	@Override
	public HistoryTaskMsgBean addHistoryTaskMsg(SessionFactory factory, String operator, String taskId,
			String operation, String comment, String attachment) {
		HistoricTaskInstance historicTask = activitiService.getHistoricTaskById(taskId);
		StaffBean staff = WorkflowUtil.getStaffByAccount(operator);
		HistoryTaskMsgBean historyTaskMsg = new HistoryTaskMsgBean();
		historyTaskMsg.setAttachment(attachment);
		historyTaskMsg.setComment(comment);
		historyTaskMsg.setId(WorkflowUtil.getObjectId());
		historyTaskMsg.setOperation(operation);
		historyTaskMsg.setOperationTime(new Date());
		historyTaskMsg.setOperationUser(operator);
		historyTaskMsg.setOperationUserName(staff.getName());
		historyTaskMsg.setProcessInstanceId(historicTask.getProcessInstanceId());
		historyTaskMsg.setNowNodeId(historicTask.getTaskDefinitionKey());
		historyTaskMsg.setNowNodeName(historicTask.getName());
		historyTaskMsg.setNowTaskId(historicTask.getId());
		historyTaskMsg.setNowNodeCode(activitiService.getTaskCode(historicTask));
		List<Task> tasks = activitiService.getTaskByProcessInsId(historicTask.getProcessInstanceId());
		if(tasks != null) {
			if(tasks.size() == 0) {
				historyTaskMsg.setNextNodeCode("");
				historyTaskMsg.setNextTaskId("");
				historyTaskMsg.setNextNodeName("结束");
			}else {
				String candidateUsers = "";
				String candidateUsersName = "";
				String nexttaskId = tasks.stream().map(item -> item.getId()).collect(Collectors.joining(","));
                String nextactivitiId = tasks.stream().map(item -> item.getTaskDefinitionKey()).collect(Collectors.joining(","));
                String nextactivitiCode = tasks.stream().map(item -> activitiService.getTaskCode(item)).collect(Collectors.joining(","));
                String nextactivitiName = tasks.stream().map(item -> item.getName()).collect(Collectors.joining(","));
                historyTaskMsg.setNextNodeId(nextactivitiId);
                historyTaskMsg.setNextNodeCode(nextactivitiCode);
                historyTaskMsg.setNextNodeName(nextactivitiName);
                historyTaskMsg.setNextTaskId(nexttaskId);
                for(Task task : tasks) {
                	List<String> c = activitiService.getCandidateUsers(task.getId());
                	List<StaffBean> staffs = WorkflowUtil.getStaffByAccount(c);
                	String username = staffs.stream().filter(t -> t != null).map(t -> t.getName()).collect(Collectors.joining(","));
                	if(candidateUsers.isEmpty()) {
                		candidateUsers = StringUtils.join(c, ",");
                		candidateUsersName = task.getName() + "(" + username + ")";
                	}else {
                		candidateUsers = candidateUsers + ", " + StringUtils.join(c, ",");
                		candidateUsersName = candidateUsersName + "," + task.getName() + "(" + username + ")";
                	}
                }
                historyTaskMsg.setNextNodeUsers(candidateUsers);
                historyTaskMsg.setNextNodeUsersName(candidateUsersName);
			}
		}
		factory.getMapper(HistoryTaskMsgMapper.class).addHistoryTaskMsg(historyTaskMsg);
		return historyTaskMsg;
	}
}
